package com.single.weblearn.web.controller;

import java.io.File;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.multipart.MultipartFile;

import com.single.weblearn.dao.ProductDao;
import com.single.weblearn.json.Uploadrs;
import com.single.weblearn.meta.Product;
import com.single.weblearn.meta.User;

@Controller
public class PublishController {
	private String fileup;
	@Autowired
	private HttpServletRequest request;
	@Autowired
	private ProductDao dao;

	@RequestMapping(value = "/public", method = RequestMethod.GET)
	public String publish() {
		HttpSession session = request.getSession();
		User user = (User) session.getAttribute("user");
		if (user!=null&&user.getUserName().equals("seller")) {
			return "public";
		} else {
			return "login";
		}

	}

	@RequestMapping(value = "/api/upload", method = RequestMethod.POST)
	public ResponseEntity<Uploadrs> upload(@RequestPart("file") MultipartFile file) {
		Uploadrs rs = new Uploadrs();
		// 判断文件是否为空
		if (!file.isEmpty()) {
			try {
				// 文件保存路径
				String filePath = request.getSession().getServletContext().getRealPath("/") + "img/"
						+ file.getOriginalFilename();
				fileup = "./img/" + file.getOriginalFilename();
				// 转存文件
				file.transferTo(new File(filePath));
				rs.setCode(200);
				rs.setMessage("success");
				rs.setResult(fileup);

			} catch (Exception e) {
				e.printStackTrace();
                //rs.setCode(500);
				//rs.setMessage("fail");
				//rs.setResult(null);
				//return new ResponseEntity<Uploadrs>(rs, HttpStatus.BAD_REQUEST);
			}
		}
		return new ResponseEntity<Uploadrs>(rs, HttpStatus.OK);
	}

	@RequestMapping(value = "/publicSubmit", method = RequestMethod.POST)
	public String publish(@RequestParam("title") String title, @RequestParam("summary") String summary,
			@RequestParam("detail") String detail, @RequestParam("price") Double price,
			@RequestParam("image") String image, @RequestParam("pic") String pic, ModelMap map) {
		Product product = new Product();
		product.setTitle(title);
		product.setSummary(summary);
		product.setDetail(detail);
		product.setPrice(price);
		if (pic.equals("file")) {
			product.setImage(fileup);
		} else {
			product.setImage(image);
		}
		dao.addProduct(product);
		map.addAttribute("product", product);
		return "publicSubmit";
	}

	@RequestMapping(value = "/edit", method = RequestMethod.GET)
	public String edit(@RequestParam("id") Integer id, ModelMap map) {
		Product p = dao.getProduct(id);
		map.addAttribute("product", p);
		return "edit";
	}

	@RequestMapping(value = "/editSubmit", method = RequestMethod.POST)
	public String editSubmit(@RequestParam("id") Integer id, @RequestParam("title") String title,
			@RequestParam("summary") String summary, @RequestParam("detail") String detail,
			@RequestParam("price") Double price, @RequestParam("image") String image, @RequestParam("pic") String pic,
			ModelMap map) {
		Product product = new Product();
		product.setId(id);
		product.setTitle(title);
		product.setSummary(summary);
		product.setDetail(detail);
		product.setPrice(price);
		if (pic.equals("file")) {
			product.setImage(fileup);
		} else {
			product.setImage(image);
		}
		dao.updateProduct(product);
		map.addAttribute("product", product);
		return "editSubmit";
	}
}